package org.csu.molediary.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.Account;
import org.csu.molediary.api.entity.Commentinfo;
import org.csu.molediary.api.entity.Good;
import org.csu.molediary.api.persistence.AccountMapper;
import org.csu.molediary.api.persistence.GoodMapper;
import org.csu.molediary.api.service.GoodService;
import org.csu.molediary.api.vo.GoodVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service("GoodService")
public class GoodServiceImpl implements GoodService {
    @Autowired
    GoodMapper goodMapper;

    @Autowired
    AccountMapper accountMapper;

    @Override
    //点赞加一
    public CommonResponse<Good> saveGood(Good good) {

        goodMapper.insert(good);
        //找出插入值
        QueryWrapper<Good> goodQueryWrapper=new QueryWrapper<>();
        goodQueryWrapper.eq("goodone",good.getGoodone()).eq("diaryid",good.getDiaryid());

        return CommonResponse.createForSuccess(goodMapper.selectOne(goodQueryWrapper));
    }

    @Override
    //通过被点赞人查找点赞信息
    public CommonResponse<List<GoodVo>> queryGoodByGooded(String Gooded) {
        QueryWrapper<Good> goodQueryWrapper=new QueryWrapper<>();
        goodQueryWrapper.eq("gooded",Gooded);
        List<Good> goods=goodMapper.selectList(goodQueryWrapper);
        List<GoodVo> goodVos=new ArrayList<>();
        for(int i=0;i<goods.size();i++){
            Account account=accountMapper.selectById(goods.get(i).getGoodone());
            GoodVo goodVo=new GoodVo();
            goodVo.setName(account.getName());
            goodVo.setHead(account.getHead());
            goodVo.setGood(goods.get(i));
            goodVos.add(goodVo);
        }
        return CommonResponse.createForSuccess(goodVos);
    }

    @Override
    //通过点赞人查找点赞信息
    public CommonResponse<List<Good>> queryGoodByGoodOne(String Goodone) {
        QueryWrapper<Good> goodQueryWrapper=new QueryWrapper<>();
        goodQueryWrapper.eq("goodone",Goodone);
        List<Good> goods=goodMapper.selectList(goodQueryWrapper);
        return CommonResponse.createForSuccess(goods);
    }
}
